﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.AdminCP.Common;
using LinkCard.Dao;
using LinkCard.Model;

namespace LinkCard.AdminCP.User
{
    public partial class user_new : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "STAFF") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }
        }
        [System.Web.Services.WebMethod]
        public static bool CheckUserName(string sUsername)
        {
            try
            {
                var db = new TopLinkEntities();
                var _user = db.Users.Where(t => t.Username == sUsername).FirstOrDefault();

                if (_user != null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                // Permission
                string sPermission1 = "0";
                string sPermission2 = "0";
                string sPermission3 = "0";
                string sPermission4 = "0";
                string sPermission5 = "0";
                string sPermission6 = "0";
                string sPermission7 = "0";

                if (Permission1.Checked) sPermission1 = "1"; // Quản lý Hệ thống
                if (Permission2.Checked) sPermission2 = "1"; // Quản lý Khách hàng
                if (Permission3.Checked) sPermission3 = "1"; // Quản lý Nhân viên
                if (Permission4.Checked) sPermission4 = "1"; // Quản lý Đối tác
                if (Permission5.Checked) sPermission5 = "1"; // Quản lý Tin bài - Trang chủ
                if (Permission6.Checked) sPermission6 = "1"; // Xem - duyệt Báo cáo

                string sPermission = "";
                sPermission = sPermission1 + sPermission2 + sPermission3 + sPermission4 + sPermission5 + sPermission6 + sPermission7;

                var dao = new UserDao();
                var _user = new Model.User();

                _user.Username = Username.Value;
                
                var passTemp = Utils.EncryptPass.Encrypt(Password.Value, Username.Value);
                _user.Password = passTemp;

                _user.Permission = sPermission;
                _user.Description = Description.Value;
                _user.Fullname = Fullname.Value;
                //_user.MobilePhone = MobilePhone.Value;
                _user.Address = Address.Value;
                _user.Email = Email.Value;
                _user.Type = "NORMAL";
                dao.Add(_user);
                Response.Redirect("/message_box.aspx?msg=8", false);

            }catch(Exception ex){
                Response.Redirect("/message_box.aspx?msg=6", false);
            }

        }
    }
}